package com.chuanyang.driverh.mainmy.adapter;

import android.content.Context;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.chuanyang.driverh.R;
import com.chuanyang.driverh.base.OnCustomListener;
import com.chuanyang.driverh.responseresult.CarTeamMenmberData;
import com.chuanyang.driverh.utils.BitmapUtil;
import com.chuanyang.driverh.utils.widget.SwipeLayout;

import java.util.ArrayList;


public class MemberAdapter extends BaseAdapter {


    public void setMenmberlist(ArrayList<CarTeamMenmberData> menmberlist) {
        this.menmberlist = menmberlist;
        notifyDataSetChanged();
    }

    private ArrayList<CarTeamMenmberData> menmberlist;
    private ArrayList<SwipeLayout> openedItems = new ArrayList<SwipeLayout>();
    private TextView tv_delete;
    private Context context;
    private OnCustomListener listener;
    private ViewHolder holder;

    public MemberAdapter(Context context, ArrayList<CarTeamMenmberData> menmberlist) {
        this.context = context;
        this.menmberlist = menmberlist;

    }

    @Override
    public int getCount() {
        return menmberlist.size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = View.inflate(parent.getContext(), R.layout.my_item_settingmenmber, null);
            holder = new ViewHolder();
            holder.iv_menmber_icon = (ImageView) convertView.findViewById(R.id.iv_menmber_icon);
            holder.tv_act_menmbername = (TextView) convertView.findViewById(R.id.tv_act_menmbername);
            holder.tv_menmber_standard = (TextView) convertView.findViewById(R.id.tv_menmber_standard);
            holder.tv_menmber_plantnum = (TextView) convertView.findViewById(R.id.tv_menmber_plantnum);
            holder.tv_menmber_cartype = (TextView) convertView.findViewById(R.id.tv_menmber_cartype);
            holder.tv_delete = (TextView) convertView.findViewById(R.id.tv_delete);
            holder.ll_div = (LinearLayout) convertView.findViewById(R.id.ll_div);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        CarTeamMenmberData bean = menmberlist.get(position);
        BitmapUtil.showImgForNetPath(bean.getUserImage(), holder.iv_menmber_icon);
        holder.tv_act_menmbername.setText(bean.getUsername());
        holder.tv_menmber_standard.setText(bean.getCarStandard());
        holder.tv_menmber_plantnum.setText(bean.getPlateNumber());
        holder.tv_menmber_cartype.setText(bean.getCarType());

        TextView tv_name = (TextView) convertView.findViewById(R.id.tv_act_menmbername);
        tv_delete = (TextView) convertView.findViewById(R.id.tv_delete);
        holder.tv_delete.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                if (listener != null) {
                    listener.onCustomerListener(holder.tv_delete, position);
                }

            }
        });
        holder.iv_menmber_icon.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (listener != null) {
                    listener.onCustomerListener(holder.iv_menmber_icon, position);
                }
            }
        });
        holder.ll_div.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (listener != null) {
                    listener.onCustomerListener(v, position);
                }
            }
        });


        SwipeLayout sl = (SwipeLayout) convertView.findViewById(R.id.sl);
        sl.setOnSwipeListener(new MySwipeListener());

        return convertView;
    }

    public void closeAllItems() {
        // 关闭当前所有打开的条目
        for (int i = 0; i < openedItems.size(); i++) {
            openedItems.get(i).close();
        }

        // 集合清除
        openedItems.clear();
    }

    class MySwipeListener implements SwipeLayout.OnSwipeListener {

        @Override
        public void onOpen(SwipeLayout layout) {
            // 记录当前被开打的条目
            openedItems.add(layout);
        }

        @Override
        public void onClose(SwipeLayout layout) {
            // 关闭了就从集合中移除
            openedItems.remove(layout);
        }

        @Override
        public void onStartOpen(SwipeLayout layout) {
            // 新的条目将要打开
            closeAllItems();
        }

        @Override
        public void onStartClose(SwipeLayout layout) {

        }
    }

    /**
     * 设置适配器上，某个控件的监听事件
     *
     * @param listener
     */
    public void setOnCustomListener(OnCustomListener listener) {
        this.listener = listener;
    }

    class ViewHolder {
        private ImageView iv_menmber_icon;
        private TextView tv_act_menmbername;
        private TextView tv_menmber_standard;
        private TextView tv_menmber_plantnum;
        private TextView tv_delete;
        private TextView tv_menmber_cartype;
        private LinearLayout ll_div;
    }
}
